# coding=utf-8

from flask import current_app
import os

from app import oraConnAC1, oraConnAC3
from app.close_ssh import close_ssh
from app.conn_ssh import conn_ssh
from app.execute_sql import execute_sql

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'


# 数据库表空间数据获取
def get_TableSpace_datas(par_sql):
    datas = []
    datas_name = []
    datas_a = []
    datas_b = []

    resultA = execute_sql(oraConnAC1, par_sql[0])  # zgdba
    for i in range(len(resultA)):
        datas_name.append(resultA[i][0])
        tablespace_datas_a = {'value': resultA[i][1], 'name': resultA[i][0]}
        datas_a.append(tablespace_datas_a)

    resultB = execute_sql(oraConnAC3, par_sql[1])  # zgdbb
    for i in range(len(resultB)):
        tablespace_datas_b = {'value': resultB[i][1], 'name': resultB[i][0]}
        datas_b.append(tablespace_datas_b)

    datas.append(datas_a)
    datas.append(datas_b)
    datas.append(datas_name)
    # current_app.logger.info("datas" + str(datas))
    current_app.logger.info("==========================================================")
    return datas


# 主机CUP、内存实时使用数据获取
def get_Real_CpuAndMem_datas(hostId, monitorHosts, monitorCommand):
    resultDatas = []

    host = monitorHosts[int(hostId)]
    ssh = conn_ssh(host)

    stdin, stdout, stderr = ssh.exec_command(str(monitorCommand[2]))
    stdoutStr = str(stdout.read())
    all_datas = stdoutStr.split('\n')[1].split('%')
    # current_app.logger.info("------------all_datas：%s", all_datas)
    cpu_real_data = round(100 - float(all_datas[0]), 2)
    current_app.logger.info("------------cpu_real_data：%s", cpu_real_data)
    resultDatas.append(cpu_real_data)

    stdin, stdout, stderr = ssh.exec_command(str(monitorCommand[3]))
    stdoutStr = str(stdout.read())
    all_datas = stdoutStr.split('k')
    # current_app.logger.info("------------all_datas：%s", all_datas)
    mem_real_data = round(float(all_datas[1])/float(all_datas[0])*100, 1)
    current_app.logger.info("------------mem_real_data：%s", mem_real_data)
    resultDatas.append(mem_real_data)

    close_ssh(ssh)  # 关闭连接
    current_app.logger.info("==========================================================")
    return resultDatas


# 主机CUP、内存趋势使用数据获取
def get_CpuAndMem_datas(hostId, monitorHosts, monitorCommand):
    resultDatas = []

    host = monitorHosts[int(hostId)]
    ssh = conn_ssh(host)

    stdin, stdout, stderr = ssh.exec_command(str(monitorCommand[4]))
    stdoutStr = str(stdout.read())
    all_datas = stdoutStr.split('\n')
    time_datas = []
    for j in range(3, len(all_datas)):
        data = all_datas[j]
        # current_app.logger.info("------------len(time_datas)：%s", len(data))
        # current_app.logger.info("------------time_datas：%s", data)
        if len(data) == 12:
            data_famate = data[0:2] + ':' + data[4:6] + ':' + data[8:10]
            # current_app.logger.info("------------time_datas：%s", data_famate)
            time_datas.append(data_famate)  # 取时间
    resultDatas.append(time_datas)

    stdin, stdout, stderr = ssh.exec_command(str(monitorCommand[0]))
    stdoutStr = str(stdout.read())
    all_datas = stdoutStr.split('\n')
    cpu_datas = []
    for j in range(3, len(all_datas)):
        data = all_datas[j]
        # current_app.logger.info("------------len(cpu_datas)：%s", len(data))
        # current_app.logger.info("------------cpu_datas：%s", data)
        if len(data) == 5:
            cpu_datas.append(round(100 - float(data), 2))  # 只取CPU已使用率
    resultDatas.append(cpu_datas)

    stdin, stdout, stderr = ssh.exec_command(str(monitorCommand[1]))
    stdoutStr = str(stdout.read())
    all_datas = stdoutStr.split('\n')
    mem_datas = []
    for j in range(3, len(all_datas)):
        data = all_datas[j]
        # current_app.logger.info("------------len(mem_datas)：%s", len(data))
        # current_app.logger.info("------------mem_datas：%s", data)
        if len(data) == 5:
            mem_datas.append(data)  # 只取内存已使用率
    resultDatas.append(mem_datas)

    close_ssh(ssh)  # 关闭连接
    current_app.logger.info("==========================================================")
    return resultDatas
